package cn.virens.server.netty_tcp.exception;

public class HandlerException extends RuntimeException {

	private static final long serialVersionUID = 7595477192082994944L;

	private String code;
	private String message;

	public HandlerException(Throwable cause) {
		this.setCause(cause);
	}

	public HandlerException(int code, String message) {
		this.setCode(Integer.toString(code));
		this.setMessage(message);
	}

	public HandlerException(String code, String message) {
		this.setMessage(message);
		this.setCode(code);
	}

	public HandlerException(int code, String message, Throwable cause) {
		this.setCode(Integer.toString(code));
		this.setMessage(message);
		this.setCause(cause);
	}

	public HandlerException(String code, String message, Throwable cause) {
		this.setMessage(message);
		this.setCause(cause);
		this.setCode(code);
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String getMessage() {
		return (null != message) ? message : super.getMessage();
	}

	public void setCause(Throwable cause) {
		if (cause instanceof HandlerException) {
			this.initCause(cause);
			this.setCode(((HandlerException) cause).getCode());
			this.setMessage(((HandlerException) cause).getMessage());
		} else if (null != cause) {
			this.initCause(cause);
		} else {
		}
	}
}
